package com.pingtai.common.action;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.pingtai.common.entity.IndentStateNumber;
import com.pingtai.common.entity.IndexPointOut;
import com.pingtai.financial.service.FinancialService;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.service.WeiboIndentService;
import com.pingtai.manage.service.ProduceManageService;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.util.Utils;

public class CommonAction {

	private UserManagerService userManagerService;
	private WeiboIndentService weiboIndentService;
	private User curUser;
	private Float theMoneyOfChildens;
	private Float rebate;
	private Float runningRebate;
	private Float rebateYesterday;
	private List stateNumberList;
	private ProduceManageService produceManageService;
	private IndexPointOut indexPointOut;
	private Integer hasIndentNoAudit=0;
	
	public IndexPointOut getIndexPointOut() {
		return indexPointOut;
	}



	public Integer getHasIndentNoAudit() {
		return hasIndentNoAudit;
	}



	public void setHasIndentNoAudit(Integer hasIndentNoAudit) {
		this.hasIndentNoAudit = hasIndentNoAudit;
	}



	public void setIndexPointOut(IndexPointOut indexPointOut) {
		this.indexPointOut = indexPointOut;
	}



	public ProduceManageService getProduceManageService() {
		return produceManageService;
	}



	public void setProduceManageService(ProduceManageService produceManageService) {
		this.produceManageService = produceManageService;
	}



	public List getStateNumberList() {
		return stateNumberList;
	}



	public void setStateNumberList(List stateNumberList) {
		this.stateNumberList = stateNumberList;
	}



	public UserManagerService getUserManagerService() {
		return userManagerService;
	}



	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}



	public WeiboIndentService getWeiboIndentService() {
		return weiboIndentService;
	}



	public void setWeiboIndentService(WeiboIndentService weiboIndentService) {
		this.weiboIndentService = weiboIndentService;
	}



	public User getCurUser() {
		return curUser;
	}



	public void setCurUser(User curUser) {
		this.curUser = curUser;
	}



	public Float getTheMoneyOfChildens() {
		return theMoneyOfChildens;
	}



	public void setTheMoneyOfChildens(Float theMoneyOfChildens) {
		this.theMoneyOfChildens = theMoneyOfChildens;
	}



	public Float getRebate() {
		return rebate;
	}



	public void setRebate(Float rebate) {
		this.rebate = rebate;
	}



	public Float getRunningRebate() {
		return runningRebate;
	}



	public void setRunningRebate(Float runningRebate) {
		this.runningRebate = runningRebate;
	}



	public Float getRebateYesterday() {
		return rebateYesterday;
	}



	public void setRebateYesterday(Float rebateYesterday) {
		this.rebateYesterday = rebateYesterday;
	}


	private int currentPage;
	private int pageNum;
	private PaginationSupport paginationSupport;
    private FinancialService financialService;
    
    private PaginationSupport paginationSupportForAudit;
	
	

	public PaginationSupport getPaginationSupportForAudit() {
		return paginationSupportForAudit;
	}



	public void setPaginationSupportForAudit(
			PaginationSupport paginationSupportForAudit) {
		this.paginationSupportForAudit = paginationSupportForAudit;
	}



	public FinancialService getFinancialService() {
		return financialService;
	}



	public void setFinancialService(FinancialService financialService) {
		this.financialService = financialService;
	}



	public int getCurrentPage() {
		return currentPage;
	}



	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}



	public int getPageNum() {
		return pageNum;
	}



	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}



	public PaginationSupport getPaginationSupport() {
		return paginationSupport;
	}



	public void setPaginationSupport(PaginationSupport paginationSupport) {
		this.paginationSupport = paginationSupport;
	}

	private List indentStateNumberList;
	
	

	public List getIndentStateNumberList() {
		return indentStateNumberList;
	}



	public void setIndentStateNumberList(List indentStateNumberList) {
		this.indentStateNumberList = indentStateNumberList;
	}



	public String defaultUI(){
		curUser = userManagerService.findCurrectUser();
		theMoneyOfChildens = userManagerService.getAllChildrensMoney( curUser,0f);
		//rebate = weiboIndentService.getAllRebateMoneyOfChildren(curUser,0f,2);
		List fanli = userManagerService.findUserfanliList(curUser);
    	Float fanliMoney = 0f;
    	if(fanli!=null&&fanli.size()>0){
    		for(int i=0;i<fanli.size();i++){
    			UserPay userPay = (UserPay) fanli.get(i);
    			fanliMoney +=userPay.getMoney();
    		}
    	}
    	rebate = fanliMoney;
		runningRebate = weiboIndentService.getAllRebateMoneyOfChildren(curUser,0f,1);
		Date date = new Date();
		Calendar calendar = new GregorianCalendar();
		 calendar.setTime(date);
		 int year = calendar.get(Calendar.YEAR);
		 int month = calendar.get(Calendar.MONTH);
		 int day = calendar.get(Calendar.DAY_OF_MONTH);
		 Calendar begin = new GregorianCalendar();
		 Calendar end = new GregorianCalendar();
		 begin.set(year, month, day-1, 0, 0);
		 end.set(year, month, day-1, 23, 59,59);
		 date=calendar.getTime(); 
		 rebateYesterday = weiboIndentService.getAllRebateMoneyOfChildren(curUser,0f,2,begin.getTime(),end.getTime());
		 rebateYesterday += weiboIndentService.getAllRebateMoneyOfChildren(curUser,0f,6,begin.getTime(),end.getTime());
		 paginationSupport = financialService.getFinancialRecords(0,curUser);
		 if(userManagerService.isAudit(curUser)){
			 List baseList = new ArrayList();
			 Iterator baseIt =  curUser.getUserIndentType().iterator();
			 while(baseIt.hasNext()){
				 UserIndentType indentType = (UserIndentType) baseIt.next();
				 int baseId = indentType.getUserBaseIndentType().getId();
				 
				 baseList.add(baseId);
			 }
			 
			 for(int i=0;i<baseList.size();i++){
				 Integer id= (Integer) baseList.get(i);
				PaginationSupport paginationSupportForAuditTmp =
						weiboIndentService.getIndentForPage(0,true,id);
				if(paginationSupportForAuditTmp!=null){
					if(paginationSupportForAudit==null) 
						paginationSupportForAudit = new PaginationSupport();
					paginationSupportForAudit.setItems(
							paginationSupportForAuditTmp.getItems());
				}
			 }
		 }
		 List indentTypeList = weiboIndentService.findAllUserIndentTypeNoParent(curUser);
		 indentTypeList = Utils.menuSort(indentTypeList);
		 for(int i=0;i<indentTypeList.size();i++){
			 Long paidui = 0l;
			 Long zhixing = 0l;
			 Long wancheng = 0l;
			 //Long tuikuan = 0l;
			 UserIndentType indentType = (UserIndentType) indentTypeList.get(i);
			 UserBaseIndentType baseIndentType = indentType.getUserBaseIndentType();
			 IndentStateNumber stateNumber = new IndentStateNumber();
			 stateNumber.setBaseIndentType(baseIndentType);
			 
			 
			 if(baseIndentType.getStateType().equals("default")){
				  paidui += weiboIndentService.getUserIndentNumberByStateAndBase(curUser,0, baseIndentType);
				  zhixing += weiboIndentService.getUserIndentNumberByStateAndBase(curUser, 1, baseIndentType);
				  wancheng += weiboIndentService.getUserIndentNumberByStateAndBase(curUser, 2, baseIndentType);
				  //tuikuan += weiboIndentService.getUserIndentNumberByStateAndBase(curUser,3, baseIndentType);
				 stateNumber.setPaidui(paidui);
				 stateNumber.setZhixing(zhixing);
				 stateNumber.setWancheng(wancheng);
				 //stateNumber.setTuikuan(tuikuan);
			 }else if(baseIndentType.getStateType().equals("renzheng")){
				 Long rz_paidui = weiboIndentService.getUserIndentNumberByStateAndBase( curUser,0, baseIndentType);
				 Long rz_shenhe = weiboIndentService.getUserIndentNumberByStateAndBase(curUser,1, baseIndentType);
				 Long rz_renzheng = weiboIndentService.getUserIndentNumberByStateAndBase(curUser, 2, baseIndentType);
				 //Long rz_bohui = weiboIndentService.getUserIndentNumberByStateAndBase(curUser, 3, baseIndentType);
				 //Long rz_jin = weiboIndentService.getUserIndentNumberByStateAndBase(curUser, 4, baseIndentType);
				 Long rz_wancheng = weiboIndentService.getUserIndentNumberByStateAndBase(curUser, 6, baseIndentType);
				 //Long rz_tuikuan = weiboIndentService.getUserIndentNumberByStateAndBase(curUser,5, baseIndentType);
				 stateNumber.setRz_paidui(rz_paidui);
				 stateNumber.setRz_shenhe(rz_shenhe);
				 stateNumber.setRz_renzheng(rz_renzheng);
				 //stateNumber.setRz_bohui(rz_bohui);
				 //stateNumber.setRz_jin(rz_jin);
				 stateNumber.setRz_wancheng(rz_wancheng);
				 //stateNumber.setRz_tuikuan(rz_tuikuan);
			 } 
		 if(stateNumberList==null){
			 stateNumberList = new ArrayList();
		 }
			if(hasIndentNoAudit==0){
				Long i_num = stateNumber.getPaidui();
				if(i_num!=null&&i_num>0){
					hasIndentNoAudit=1;
				}
			}
			 stateNumberList.add(stateNumber);
		 }
		 indexPointOut = produceManageService.getIndexPointOut();
		 return "success";
	}
	
	private Integer base;
	private Integer day;

	public Integer getBase() {
		return base;
	}



	public void setBase(Integer base) {
		this.base = base;
	}



	public Integer getDay() {
		return day;
	}



	public void setDay(Integer day) {
		this.day = day;
	}
	
	private UserBaseIndentType baseIndentType;
	
	
	
	public UserBaseIndentType getBaseIndentType() {
		return baseIndentType;
	}



	public void setBaseIndentType(UserBaseIndentType baseIndentType) {
		this.baseIndentType = baseIndentType;
	}



	public String clearIndent(){
		if(base==4||base==5||base==15||base==45||base==11){
			baseIndentType = weiboIndentService.getBaseIndentTypeById(base);
			List baseList = new ArrayList();
			Set childBaseIndentType = baseIndentType.getChildrenBaseIndentType();
			Iterator childIt = childBaseIndentType.iterator();
			while (childIt.hasNext()) {
				UserBaseIndentType ubt = (UserBaseIndentType) childIt.next();
				baseList.add(ubt.getId());
				Set ch = ubt.getChildrenBaseIndentType();
				Iterator chIt = ch.iterator();
				while (chIt.hasNext()) {
					UserBaseIndentType ubt2 = (UserBaseIndentType) chIt.next();
					baseList.add(ubt2.getId());
				}
			}
			baseList.add(baseIndentType.getId());
			
			weiboIndentService.deleteAllIndentByBase(baseList, 5,day);
			weiboIndentService.deleteAllIndentByBase(baseList, 6,day);
			weiboIndentService.deleteAllIndentByBase(baseList, 2,day);
		}
		return "success";
	}
}
